<template>
  <div class="back-html">
    <!-- 装饰性背景元素 -->
    <div class="decoration-bg">
      <div class="floating-line line-1"></div>
      <div class="floating-line line-2"></div>
    </div>

    <el-row class="home-container">
      <!-- 归档内容部分 -->
      <el-col :lg="{span: 18, offset: 0}" :span="24">
        <div class="archive-content">

          <el-card class="archive-card">
            <!-- 归档标题和描述 -->
            <div class="archive-header">
              <h2 class="archive-title">
                <i class="icon-archive-fill"></i>
                文章归档
              </h2>
              <p class="archive-description">按时间顺序记录每一篇文章的创作历程</p>
            </div>

            <!-- 按年份分组的归档内容 -->
            <div class="archive-years">
              <div
                v-for="yearGroup in articlesByYear"
                :key="yearGroup.year"
                class="year-group"
              >
                <!-- 年份标题 -->
                <div class="year-header">
                  <div class="year-title" @click="toggleYear(yearGroup.year)">
                    <i class="icon-calendar-fill"></i>
                    <span class="year-text">{{ yearGroup.year }}</span>
                    <div class="year-count">{{ yearGroup.count }}篇文章</div>
                    <el-icon class="collapse-icon" :class="{ 'collapsed': !expandedYears[yearGroup.year] }">
                      <ArrowDown />
                    </el-icon>
                  </div>
                  <div class="year-line"></div>
                </div>

                <!-- 该年份的文章时间线 -->
                <el-collapse-transition>
                  <el-timeline 
                    v-show="expandedYears[yearGroup.year]"
                    class="year-timeline"
                  >
                    <el-timeline-item
                        v-for="(activity, index) in yearGroup.articles"
                        :key="activity.id"
                        :timestamp="formatDate(activity.createTime)"
                        class="timeline-item"
                    >
                    <div class="article-timeline-content">
                      <div class="article-main-info">
                        <router-link
                            :to="'/articles/' + activity.id"
                            class="article-title-link"
                        >
                          {{ activity.title }}
                        </router-link>
                        <div class="article-summary" v-if="activity.summary">
                          {{ activity.summary }}
                        </div>
                      </div>

                      <div class="article-meta-info">
                        <div class="meta-item" v-if="activity.categoryName">
                          <el-icon color="#ff69b4"><Folder/></el-icon>
                          <span>{{ activity.categoryName }}</span>
                        </div>
                        <div class="meta-item" v-if="activity.viewCounts">
                          <el-icon color="#4ecdc4"><View/></el-icon>
                          <span v-if="activity.viewCounts >= 1000 && activity.viewCounts < 10000">{{ Math.floor(activity.viewCounts / 1000) }}k</span>
                          <span v-else-if="activity.viewCounts >= 10000">{{ Math.floor(activity.viewCounts / 10000) }}w</span>
                          <span v-else>{{ activity.viewCounts }}</span>
                        </div>
                        <div class="meta-item" v-if="activity.readTime">
                          <el-icon color="#f39c12"><Timer/></el-icon>
                          <span>{{ activity.readTime }}分钟</span>
                        </div>
                        <div class="meta-item" v-if="activity.isWeight === 1">
                          <el-tag type="success" size="small">置顶</el-tag>
                        </div>
                      </div>

                      <!-- 文章标签 -->
                      <div class="article-tags" v-if="activity.tags && activity.tags.length > 0">
                        <el-tag
                          v-for="tag in activity.tags"
                          :key="tag"
                          size="small"
                          class="tag-item"
                          effect="plain"
                        >
                          {{ tag }}
                        </el-tag>
                      </div>
                    </div>
                    </el-timeline-item>
                  </el-timeline>
                </el-collapse-transition>
              </div>
            </div>
<!--            <el-pagination-->
<!--                v-model:currentPage="currentPage"-->
<!--                :page-size="pageSize"-->
<!--                :small="false"-->
<!--                :disabled="false"-->
<!--                :background="true"-->
<!--                layout="prev, pager, next"-->
<!--                :total="totalSize"-->
<!--                @current-change="handleCurrentChange"-->
<!--                class="custom-pagination"-->
<!--            />-->
          </el-card>
        </div>
      </el-col>

      <!-- 右侧边栏 -->
      <el-col :lg="{span: 6, offset: 0}" :span="0" v-if="blogInfo">
        <div class="right-sidebar">
          <!-- Profile Card -->
          <ProfileCard :blogInfo="blogInfo"/>

          <!-- Announcement Card -->
          <AnnouncementCard :blogInfo="blogInfo"/>

          <!-- Recommended Articles -->
          <ArticleCard :articleList="recentArticles"/>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import request from 'utils/request.js'
import {markRaw, nextTick, onMounted, ref, computed} from "vue";
import ProfileCard from "../../components/ProfileCard.vue";
import ArticleCard from "../../components/RecommendedArticles.vue";
import AnnouncementCard from "../../components/AnnouncementCard.vue";
import {Folder, View, Timer, ArrowDown} from '@element-plus/icons-vue';

interface Article {
  id: number;
  title: string;
  summary: string;
  coverImage: string;
  createTime: string;
  viewCounts: number;
  isWeight: number;
  categoryId: number;
  categoryName: string;
  tags: string[];
  readTime: number;
}

interface BlogInfo {
  webSite: {
    title?: string;
  };
}

const activities = ref([])
const blogInfo = ref<BlogInfo>({
  webSite: {}
});
const recentArticles = ref<Article[]>([]);

// 分页数据
const currentPage = ref(1);
const pageSize = ref(999)
const totalSize = ref(0);

// 年份展开/收起状态
const expandedYears = ref<{ [key: string]: boolean }>({});

// 按年份分组的文章数据
const articlesByYear = computed(() => {
  if (!activities.value.length) return [];

  const groupedByYear: { [key: string]: any[] } = {};

  activities.value.forEach((article: any) => {
    const year = new Date(article.createTime).getFullYear().toString();
    if (!groupedByYear[year]) {
      groupedByYear[year] = [];
    }
    groupedByYear[year].push(article);
  });

  // 按年份降序排列
  const yearGroups = Object.keys(groupedByYear)
    .sort((a, b) => parseInt(b) - parseInt(a))
    .map(year => ({
      year: year,
      articles: groupedByYear[year].sort((a, b) =>
        new Date(b.createTime).getTime() - new Date(a.createTime).getTime()
      ),
      count: groupedByYear[year].length
    }));
  
  // 初始化展开状态（默认所有年份都展开）
  yearGroups.forEach(yearGroup => {
    if (!(yearGroup.year in expandedYears.value)) {
      expandedYears.value[yearGroup.year] = true;
    }
  });
  
  return yearGroups;
});


const getArticles = ()=>{
  request.get("/blog/getArticles",{
    params:{
      pageNum: currentPage.value,
      pageSize: pageSize.value
    }
  }).then((res:any)=>{
    activities.value = res.data.records
    totalSize.value = res.data.total
  })
}

const getBlogInfo = () => {
  request.get("/index/getBlogInfo").then((res: any) => {
    nextTick(() => {
      blogInfo.value = markRaw(res.data);
    })
  })
}

const getRecentArticles = () => {
  request.get("/blog/getArticles", {
    params: {
      pageNum: 1,
      pageSize: 5,
      title: '',
      categoryId: null
    }
  }).then((res: any) => {
    nextTick(() => {
      recentArticles.value = markRaw(res.data.records);
    })
  })
}

const formatDate = (dateString: string) => {
  const date = new Date(dateString);
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  return `${month}-${day}`;
}

// 切换年份的展开/收起状态
const toggleYear = (year: string) => {
  expandedYears.value[year] = !expandedYears.value[year];
}

const handleCurrentChange = (val: number) => {
  currentPage.value = val;
  getArticles();
}

onMounted(()=>{
  getArticles()
  getBlogInfo()
  getRecentArticles()
})
</script>

<style scoped lang="scss">
@import '@/assets/styles/variables.scss';

.back-html {
  background: var(--gradient-page);
  min-height: 100vh;
  padding-top: 10px;
  margin: 0 10px 10px 10px;
  position: relative;
  overflow: hidden;
}

// 装饰性背景元素
.decoration-bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
}

.floating-line {
  position: absolute;
  background: var(--bg-decoration);
  animation: lineFloat 8s ease-in-out infinite;
}

.line-1 {
  width: 200px;
  height: 2px;
  top: 30%;
  right: 5%;
  transform: rotate(45deg);
  animation-delay: 1s;
}

.line-2 {
  width: 150px;
  height: 2px;
  bottom: 40%;
  left: 8%;
  transform: rotate(-30deg);
  animation-delay: 3s;
}

@keyframes lineFloat {
  0%, 100% {
    transform: translateX(0px) rotate(45deg);
    opacity: 0.6;
  }
  50% {
    transform: translateX(10px) rotate(50deg);
    opacity: 0.3;
  }
}

.banner {
  height: 300px;
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  margin-bottom: 40px;
  z-index: 2;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.3);
  }
}

.banner-title {
  font-size: 3rem;
  color: white;
  font-weight: bold;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
  position: relative;
  z-index: 3;
}

.home-container {
  display: flex;
  width: 100%;
  margin: 0;
  padding: 0 40px;
  animation: card_fadenum 0.8s;
  min-height: calc(100vh - 380px);
  position: relative;
  z-index: 2;
}

.archive-content {
  width: 100%;
}


.archive-card {
  background: var(--bg-card);
  border-radius: 20px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(227, 242, 253, 0.3);
  padding: 30px;
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 4px;
    background: var(--gradient-rainbow);
  }
}

// 归档标题区域
.archive-header {
  margin-bottom: 30px;
  text-align: center;
  padding-bottom: 20px;
  border-bottom: 2px solid rgba(227, 242, 253, 0.3);
}

.archive-title {
  font-size: 28px;
  font-weight: bold;
  color: var(--text-primary);
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;

  i {
    color: var(--primary-color);
    font-size: 30px;
  }
}

.archive-description {
  font-size: 16px;
  color: var(--text-secondary);
  margin: 0;
  font-style: italic;
}

// 年份分组样式
.archive-years {
  display: flex;
  flex-direction: column;
  gap: 40px;
}

.year-group {
  position: relative;
}

.year-header {
  display: flex;
  align-items: center;
  margin-bottom: 25px;
  position: relative;
}

.year-title {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg-card);
  padding: 12px 20px;
  border-radius: 25px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  border: 2px solid var(--primary-color);
  position: relative;
  z-index: 2;
  flex-shrink: 0;
  cursor: pointer;
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
    border-color: var(--primary-dark);
  }

  i {
    color: var(--primary-color);
    font-size: 20px;
  }
}

.collapse-icon {
  margin-left: auto;
  transition: transform 0.3s ease;
  color: var(--primary-color);
  font-size: 16px;

  &.collapsed {
    transform: rotate(-90deg);
  }
}

.year-text {
  font-size: 24px;
  font-weight: bold;
  color: var(--primary-color);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.year-count {
  background: var(--gradient-accent);
  color: white;
  padding: 4px 12px;
  border-radius: 15px;
  font-size: 12px;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.year-line {
  flex: 1;
  height: 2px;
  background: linear-gradient(to right, var(--primary-color), transparent);
  margin-left: 20px;
  position: relative;

  &::after {
    content: '';
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 8px;
    height: 8px;
    background: var(--primary-color);
    border-radius: 50%;
  }
}

.year-timeline {
  margin-left: 20px;
  position: relative;

  &::before {
    content: '';
    position: absolute;
    left: 8px;
    top: 0;
    bottom: 0;
    width: 3px;
    background: var(--gradient-primary);
    border-radius: 2px;
    z-index: 1;
  }
}

.timeline-item {
  padding: 20px 0;

  :deep(.el-timeline-item__wrapper) {
    padding-left: 40px;
  }

  :deep(.el-timeline-item__timestamp) {
    color: var(--text-secondary);
    font-size: 14px;
    font-weight: 600;
    background: var(--bg-secondary);
    padding: 4px 12px;
    border-radius: 12px;
    border: 1px solid rgba(227, 242, 253, 0.5);
  }

  :deep(.el-timeline-item__node) {
    background-color: var(--primary-color);
    border: 4px solid var(--bg-card);
    box-shadow: 0 3px 12px rgba(0, 0, 0, 0.15);
    width: 16px;
    height: 16px;
  }

  :deep(.el-timeline-item__tail) {
    border-left: 3px solid var(--bg-border);
  }
}

// 文章时间线内容
.article-timeline-content {
  background: var(--bg-secondary);
  border-radius: 15px;
  padding: 20px;
  border: 1px solid rgba(227, 242, 253, 0.3);
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 4px;
    height: 100%;
    background: var(--gradient-accent);
  }

  &:hover {
    transform: translateX(8px);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1);
    border-color: var(--primary-color);
  }
}

.article-main-info {
  margin-bottom: 15px;
}

.article-title-link {
  color: var(--text-primary);
  text-decoration: none;
  font-size: 18px;
  font-weight: 700;
  transition: all 0.3s ease;
  display: block;
  margin-bottom: 8px;

  &:hover {
    color: var(--primary-color);
  }
}

.article-summary {
  color: var(--text-secondary);
  font-size: 14px;
  line-height: 1.6;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  margin-bottom: 10px;
}

.article-meta-info {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  margin-bottom: 12px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 13px;
  color: var(--text-secondary);
  font-weight: 500;

  span {
    font-weight: 600;
  }
}

.article-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.tag-item {
  background: rgba(255, 105, 180, 0.1);
  border: 1px solid rgba(255, 105, 180, 0.3);
  color: #ff69b4;
  font-size: 12px;
  transition: all 0.3s ease;

  &:hover {
    background: rgba(255, 105, 180, 0.2);
    transform: scale(1.05);
  }
}

.custom-pagination {
  margin-top: 30px;
  margin-bottom: 20px;
  display: flex;
  justify-content: center;

  :deep(.el-pagination) {
    .el-pager li {
      border-radius: 20px !important;
      margin: 0 6px !important;
      font-size: 16px !important;
      font-weight: 600 !important;
      background: white !important;
      border: 2px solid #e3f2fd !important;
      color: #666 !important;
      min-width: 40px !important;
      height: 40px !important;

      &:hover {
        border-color: #90caf9 !important;
        color: #1976d2 !important;
      }

      &.is-active {
        background: var(--bg-hover) !important;
        border-color: #1976d2 !important;
        color: #1976d2 !important;
      }
    }

    .btn-prev, .btn-next {
      border-radius: 20px !important;
      margin: 0 6px !important;
      font-size: 16px !important;
      font-weight: 600 !important;
      background: white !important;
      border: 2px solid #e3f2fd !important;
      color: #666 !important;
      min-width: 40px !important;
      height: 40px !important;

      &:hover:not(:disabled) {
        border-color: #90caf9 !important;
        color: #1976d2 !important;
      }

      &:disabled {
        opacity: 0.4 !important;
      }
    }

  }
}

.right-sidebar {
  flex: 6;
  display: flex;
  flex-direction: column;
  gap: 20px;
  margin-left: 40px;

  .el-card {
    border-radius: 15px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  }
}

@keyframes card_fadenum {
  0% {
    opacity: 0;
    transform: translateY(20px);
  }
  100% {
    opacity: 1;
    transform: translateY(0);
  }
}

// 响应式设计
@media (max-width: 992px) {
  .home-container {
    padding: 0 20px;
  }

  .banner-title {
    font-size: 2rem;
  }

  .right-sidebar {
    margin-left: 0;
    margin-top: 20px;
  }

}

@media (max-width: 768px) {
  .banner {
    height: 200px;
  }

  .banner-title {
    font-size: 1.5rem;
  }

  .home-container {
    padding: 0 15px;
  }

  .archive-card {
    padding: 20px;
  }


  .archive-title {
    font-size: 22px;
    flex-direction: column;
    gap: 5px;
  }

  .archive-years {
    gap: 30px;
  }

  .year-header {
    margin-bottom: 20px;
  }

  .year-title {
    padding: 10px 16px;
    gap: 8px;
  }

  .year-text {
    font-size: 20px;
  }

  .year-count {
    font-size: 11px;
    padding: 3px 8px;
  }

  .collapse-icon {
    font-size: 14px;
  }

  .year-line {
    margin-left: 15px;
  }

  .year-timeline {
    margin-left: 15px;
  }

  .article-timeline-content {
    padding: 15px;

    &:hover {
      transform: translateX(4px);
    }
  }

  .article-title-link {
    font-size: 16px;
  }

  .article-meta-info {
    gap: 10px;
  }

  .meta-item {
    font-size: 12px;
  }

  .timeline-item {
    :deep(.el-timeline-item__wrapper) {
      padding-left: 25px;
    }
  }
}

@media (max-width: 480px) {
  .back-html {
    margin: 0 5px 5px 5px;
  }

  .year-title {
    padding: 8px 12px;
    gap: 6px;
  }

  .year-text {
    font-size: 18px;
  }

  .year-count {
    font-size: 10px;
    padding: 2px 6px;
  }

  .collapse-icon {
    font-size: 12px;
  }

  .year-line {
    margin-left: 10px;
  }

  .year-timeline {
    margin-left: 10px;

    &::before {
      left: 6px;
      width: 2px;
    }
  }

  .timeline-item {
    :deep(.el-timeline-item__wrapper) {
      padding-left: 20px;
    }

    :deep(.el-timeline-item__node) {
      width: 12px;
      height: 12px;
    }
  }
}
</style>
